package com.imooc.spikesystem.controller;

import com.imooc.spikesystem.config.redis.KeyPrefix;
import com.imooc.spikesystem.config.redis.RedisConfig;
import com.imooc.spikesystem.config.redis.UserKey;
import com.imooc.spikesystem.domain.User;
import com.imooc.spikesystem.service.UserService;
import com.imooc.spikesystem.service.impl.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

/**
 * @author wy
 */
@RestController
@RequestMapping("/demo")
@Slf4j
public class SampleController {

    @Autowired
    private UserService userService;

    @Autowired
    private RedisService redisService;

    @RequestMapping("/thymeleaf")
    public String thymeleaf(Model model){
        model.addAttribute("name","Joshua");
        return "hello";
    }

    @GetMapping("/db")
    public User get(){
        return userService.getById(1);
    }

    @GetMapping("/redis")
    @ResponseBody
    public User getRedis(){
        User user = redisService.get(UserKey.getById, "1", User.class);
        return user;
    }

    @GetMapping("/redis/set")
    @ResponseBody
    public boolean setRedis(){
        User user = new User();
        user.setId(1);
        user.setName("da");
        boolean hello = redisService.set(UserKey.getById,"1",user);
        return true;
    }


}
